//
//  UserlLikeViewController.m
//  one·一个  tyw
//
//  Created by tyw on 15/9/9.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "UserlLikeViewController.h"
#import "DayNightManager.h"
#import "DayNightImgView.h"
#import "DayNightLabel.h"
#import "UserLikeCollectionViewCell.h"
#import "UserLikeTableViewCell.h"
#import "DataService.h"
#import "DayNightImgView.h"
#import "UserLikeHomeViewController.h"
#import "UserLikeArticleViewController.h"
#import "UserLikeQuestionViewController.h"
#import "UserLikeThingViewController.h"
#import "HomeViewController.h"
#import "ArticleViewController.h"
#import "QuestionViewController.h"
#import "ThingViewController.h"

@interface UserlLikeViewController ()

@end

@implementation UserlLikeViewController {
    UIImageView* headImgView;           //头部视图
    CGFloat headImgViewHeight;      //头部视图的高度
    
    //为了区分数据源，使用标记tag
    UICollectionView* _homeCollectionView;
    UITableView* _articleTableView;
    UITableView* _questionTableView;
    UICollectionView* _thingCollectionView;
    
    //没有内容显示时显示的图片
    UIView* _noLikeView;
    DayNightImgView* _noLikedImgView;
    
    
    //数据
    NSArray* _homeDataArray;
    NSArray* _articleDataArray;
    NSArray* _questionDataArray;
    NSArray* _thingDataArray;
    
    
    NSInteger index;        //一个标记，标记着目前显示的收藏内容的序号，比如首页0、文章1、问题2、东西3
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.userInteractionEnabled = YES;
    
    [self _createNavBar];
    
    [self _createHeadImgView];
    
    [self _createSubViews];     //创建子视图们，四个    图片和东西是collectionView，文章和问题是tableView
    
    
    
    //页面出现时，首先加载首页数据
    [self _loadHomeData];
    [self.view addSubview:_homeCollectionView];
    [_articleTableView removeFromSuperview];
    [_questionTableView removeFromSuperview];
    [_thingCollectionView removeFromSuperview];
    index = 0;
}

- (void)_createNavBar {
    self.navigationItem.titleView = nil;
    self.navigationItem.title = @"我的收藏";
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;

}


#pragma mark - HeadImgView
//头部大图
- (void)_createHeadImgView {
    headImgViewHeight = 470.0/750.0*kWidth;
    headImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWidth, headImgViewHeight)];
    headImgView.image = [UIImage imageNamed:@"collectionHeadBg"];
    headImgView.userInteractionEnabled = YES;
    [self.view addSubview:headImgView];
    
    //返回按钮
    UIButton* backBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 20, 38)];
    [backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [headImgView addSubview:backBtn];
    
    //头像
    UIImageView* userImgView = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth/2-30, 80, 60, 60)];
    userImgView.layer.cornerRadius = 30;
    userImgView.layer.masksToBounds = YES;
    userImgView.image = [UIImage imageNamed:@"1.jpg"];
    [headImgView addSubview:userImgView];
    
    //用户名
    UILabel* nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth/2-30, 140, 60, 30)];
    nameLabel.text = @"阿狸";
    nameLabel.textColor = [UIColor whiteColor];
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.font = [UIFont systemFontOfSize:15];
    [headImgView addSubview:nameLabel];
    
    //按钮
    CGFloat width = kWidth/4;
    NSArray* btnName = @[@"图片",@"文章",@"问题",@"东西"];
    for (int i = 0; i < 4; i++) {
        UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(width*i, headImgViewHeight-30, width, 30)];
        [button setTitle:btnName[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithRed:77.0/255 green:143.0/255 blue:247.0/255 alpha:1] forState:UIControlStateHighlighted];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = i+100;
        [headImgView addSubview:button];
    }
}

- (void)backAction {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)buttonAction:(UIButton*)button {
    index = button.tag-100;
    
    [_noLikeView removeFromSuperview];
    
    if (button.tag == 100) {        //图片
        [self.view addSubview:_homeCollectionView];
        [_articleTableView removeFromSuperview];
        [_questionTableView removeFromSuperview];
        [_thingCollectionView removeFromSuperview];
        
        [self _loadHomeData];
        
    } else if (button.tag == 101) {     //文章
        [self.view addSubview:_articleTableView];
        [_homeCollectionView removeFromSuperview];
        [_questionTableView removeFromSuperview];
        [_thingCollectionView removeFromSuperview];
        
        [self _loadArticleData];
        
    } else if (button.tag == 102) {     //问题
        [self.view addSubview:_questionTableView];
        [_articleTableView removeFromSuperview];
        [_homeCollectionView removeFromSuperview];
        [_thingCollectionView removeFromSuperview];
        
        [self _loadQuestionData];
        
    } else {                                //东西
        [self.view addSubview:_thingCollectionView];
        [_articleTableView removeFromSuperview];
        [_questionTableView removeFromSuperview];
        [_homeCollectionView removeFromSuperview];
        
        [self _loadThingData];
    }
}

#pragma mark - loadData
//加载数据，获得  图片收藏  的数据
- (void)_loadHomeData {
    //01 构造参数
    NSDictionary* params = @{@"strUi" : kStrUi,
                             @"strCt" : @0,
                             @"strBr" : @0,
                             @"strEr" : @99999
                             };
    
    
    //02 网络请求
    [DataService oneRequestUrl:kSeeCollectUrl httpMethod:@"GET" params:[params mutableCopy] block:^(id result) {
        if ([result isKindOfClass:[NSError class]]) {
            NSLog(@"查看首页收藏出错  %@", result);
        }
        
//        NSLog(@"%@", [Tools getJsonStrFromObj:result]);
        
        _homeDataArray = result[@"entClt"];
        
        if (_homeDataArray.count > 0) {
            [_homeCollectionView reloadData];
            index = 0;
        } else {            // 如果没有收藏的内容，就显示  没有内容显示
            [self.view addSubview:_noLikeView];
            [_homeCollectionView removeFromSuperview];
            [_articleTableView removeFromSuperview];
            [_questionTableView removeFromSuperview];
            [_thingCollectionView removeFromSuperview];
        }
    }];
}

//加载数据，获得  图片收藏  的数据
- (void)_loadArticleData {
    //01 构造参数
    NSDictionary* params = @{@"strUi" : kStrUi,
                             @"strCt" : @1,
                             @"strBr" : @0,
                             @"strEr" : @20
                             };
    
    
    //02 网络请求
    [DataService oneRequestUrl:kSeeCollectUrl httpMethod:@"GET" params:[params mutableCopy] block:^(id result) {
        if ([result isKindOfClass:[NSError class]]) {
            NSLog(@"查看文章页收藏出错  %@", result);
        }
        
        //        NSLog(@"%@", [Tools getJsonStrFromObj:result]);
        
        _articleDataArray = result[@"entClt"];
        
        if (_articleDataArray.count > 0) {
            [_articleTableView reloadData];
            index = 1;
        } else {            // 如果没有收藏的内容，就显示  没有内容显示
            [self.view addSubview:_noLikeView];
            [_homeCollectionView removeFromSuperview];
            [_articleTableView removeFromSuperview];
            [_questionTableView removeFromSuperview];
            [_thingCollectionView removeFromSuperview];
        }
    }];
}

//加载数据，获得  图片收藏  的数据
- (void)_loadQuestionData {
    //01 构造参数
    NSDictionary* params = @{@"strUi" : kStrUi,
                             @"strCt" : @2,
                             @"strBr" : @0,
                             @"strEr" : @20
                             };
    
    
    //02 网络请求
    [DataService oneRequestUrl:kSeeCollectUrl httpMethod:@"GET" params:[params mutableCopy] block:^(id result) {
        if ([result isKindOfClass:[NSError class]]) {
            NSLog(@"查看 问题页 收藏出错  %@", result);
        }
        
//        NSLog(@"%@", [Tools getJsonStrFromObj:result]);
        
        _questionDataArray = result[@"entClt"];
        
        
        if (_questionDataArray.count > 0) {
            [_questionTableView reloadData];
            index = 2;
        } else {            // 如果没有收藏的内容，就显示  没有内容显示
            [self.view addSubview:_noLikeView];
            [_homeCollectionView removeFromSuperview];
            [_articleTableView removeFromSuperview];
            [_questionTableView removeFromSuperview];
            [_thingCollectionView removeFromSuperview];
        }
    }];
}

//加载数据，获得  图片收藏  的数据
- (void)_loadThingData {
    //01 构造参数
    NSDictionary* params = @{@"strUi" : kStrUi,
                             @"strCt" : @3,
                             @"strBr" : @0,
                             @"strEr" : @99999
                             };
    
    
    //02 网络请求
    [DataService oneRequestUrl:kSeeCollectUrl httpMethod:@"GET" params:[params mutableCopy] block:^(id result) {
        if ([result isKindOfClass:[NSError class]]) {
            NSLog(@"查看 东西页 收藏出错  %@", result);
        }
        
        //        NSLog(@"%@", [Tools getJsonStrFromObj:result]);
        
        _thingDataArray = result[@"entClt"];
        
        if (_thingDataArray.count > 0) {
            [_thingCollectionView reloadData];
            index = 3;
        } else {            // 如果没有收藏的内容，就显示  没有内容显示
            [self.view addSubview:_noLikeView];
            [_homeCollectionView removeFromSuperview];
            [_articleTableView removeFromSuperview];
            [_questionTableView removeFromSuperview];
            [_thingCollectionView removeFromSuperview];
        }
    }];
}



#pragma mark - 子视图们
- (void)_createSubViews {
    [self _createHomeCollectionView];
    [self _createArticleTableView];
    [self _createQuestionTableView];
    [self _createThingCollectionView];
    [self _createNoLikedImgView];
}

- (void)_createHomeCollectionView {
    UICollectionViewFlowLayout* layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat width = kWidth/2;
    CGFloat height = width*0.7;
    layout.itemSize = CGSizeMake(width, height);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    
    _homeCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, headImgViewHeight, kWidth, kHeight-headImgViewHeight) collectionViewLayout:layout];
    _homeCollectionView.delegate = self;
    _homeCollectionView.dataSource = self;
    _homeCollectionView.tag = 100;
    _homeCollectionView.backgroundColor = [UIColor clearColor];
    _homeCollectionView.showsVerticalScrollIndicator = NO;
    
    [_homeCollectionView registerClass:[UserLikeCollectionViewCell class] forCellWithReuseIdentifier:kUserLikeCollectionViewCellId];
}

- (void)_createArticleTableView {
    _articleTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, headImgViewHeight, kWidth, kHeight-headImgViewHeight)];
    _articleTableView.delegate = self;
    _articleTableView.dataSource = self;
    _articleTableView.tag = 101;
    _articleTableView.backgroundColor = [UIColor clearColor];
    _articleTableView.showsVerticalScrollIndicator = NO;

//    UIView* tableHeadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, headImgViewHeight)];
//    tableHeadView.backgroundColor = [UIColor clearColor];
//    _articleTableView.tableHeaderView = tableHeadView;
//    tableHeadView.userInteractionEnabled = YES;
    
    [_articleTableView registerClass:[UserLikeTableViewCell class] forCellReuseIdentifier:kUserLikeTableViewCellId];
}

- (void)_createQuestionTableView {
    _questionTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, headImgViewHeight, kWidth, kHeight-headImgViewHeight)];
    _questionTableView.delegate = self;
    _questionTableView.dataSource = self;
    _questionTableView.tag = 102;
    _questionTableView.backgroundColor = [UIColor clearColor];
    _questionTableView.showsVerticalScrollIndicator = NO;
    
//    UIView* tableHeadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, headImgViewHeight)];
//    tableHeadView.backgroundColor = [UIColor clearColor];
//    _questionTableView.tableHeaderView = tableHeadView;
//    tableHeadView.userInteractionEnabled = YES;
    
    [_questionTableView registerClass:[UserLikeTableViewCell class] forCellReuseIdentifier:kUserLikeTableViewCellId];
}

- (void)_createThingCollectionView {
    UICollectionViewFlowLayout* layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat width = kWidth/2;
    CGFloat height = width*0.7;
    layout.itemSize = CGSizeMake(width, height);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    
    _thingCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, headImgViewHeight, kWidth, kHeight-headImgViewHeight) collectionViewLayout:layout];
    _thingCollectionView.delegate = self;
    _thingCollectionView.dataSource = self;
    _thingCollectionView.tag = 103;
    _thingCollectionView.backgroundColor = [UIColor clearColor];
    _thingCollectionView.showsVerticalScrollIndicator = NO;
    
    [_thingCollectionView registerClass:[UserLikeCollectionViewCell class] forCellWithReuseIdentifier:kUserLikeCollectionViewCellId];
}

- (void)_createNoLikedImgView {
    _noLikeView = [[UIView alloc] initWithFrame:CGRectMake(0, headImgViewHeight, kWidth, kHeight-headImgViewHeight)];
    _noLikeView.userInteractionEnabled = YES;
    
    CGFloat height = 200.0/312.0*kWidth;
    _noLikedImgView = [[DayNightImgView alloc] initWithFrame:CGRectMake(0, 80, kWidth, height)];
    [_noLikedImgView setImgName:@"noCollection"];
    [_noLikeView addSubview:_noLikedImgView];
    
    UIButton* goToSeeBtn = [[UIButton alloc] initWithFrame:CGRectMake(kWidth/2-50, height+100, 100, 20)];
    [_noLikeView addSubview:goToSeeBtn];
    [goToSeeBtn setTitle:@"去看看" forState:UIControlStateNormal];
    goToSeeBtn.layer.cornerRadius = 5;
    goToSeeBtn.layer.borderWidth = 1;
    goToSeeBtn.layer.borderColor = [UIColor colorWithRed:77.0/255 green:171.0/255 blue:231.0/255 alpha:1].CGColor;
    [goToSeeBtn setTitleColor:[UIColor colorWithRed:77.0/255 green:171.0/255 blue:231.0/255 alpha:1] forState:UIControlStateNormal];
    [goToSeeBtn setTitleColor:[UIColor colorWithRed:100.0/255 green:191.0/255 blue:251.0/255 alpha:1] forState:UIControlStateHighlighted];
    [goToSeeBtn addTarget:self action:@selector(goToSeeAction) forControlEvents:UIControlEventTouchUpInside];
}

//去看看
- (void)goToSeeAction {
    //根据index确定当前显示的是第几个页面。
    //发送通知，告诉tabbarController跳至相应界面
    NSDictionary* info = @{@"index" : @(index)};
    [[NSNotificationCenter defaultCenter] postNotificationName:kTabBarControllerChangeIndexNotification object:nil userInfo:info];
    
    //pop,如果没有这一句，那么当重新点回到个人页面的时候，就还是会出现在现实去看看按钮的这个界面
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark - collectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (collectionView.tag == 100) {            //如果是首页
        return _homeDataArray.count;
    } else if (collectionView.tag == 103){      //如果是东西页
        return _thingDataArray.count;
    } else {
        return -1;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UserLikeCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:kUserLikeCollectionViewCellId forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor clearColor];
    
    if (collectionView.tag == 100) {            //如果是首页
        cell.dataDic = _homeDataArray[indexPath.row];
        return cell;
        
    } else if (collectionView.tag == 103){      //如果是东西页
        cell.dataDic = _thingDataArray[indexPath.row];
        return cell;
        
    } else {
        return nil;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    if (collectionView.tag == 100) {        //如果是首页
        NSDictionary* dic = _homeDataArray[indexPath.row];
        
        UserLikeHomeViewController* userLikeHomeVC = [[UserLikeHomeViewController alloc] init];
        userLikeHomeVC.dataDic = dic;
        [self.navigationController pushViewController:userLikeHomeVC animated:YES];
        
    } else {                                //如果是东西页
        NSDictionary* dic = _thingDataArray[indexPath.row];
        
        UserLikeThingViewController* userLikeThingVC = [[UserLikeThingViewController alloc] init];
        userLikeThingVC.dataDic = dic;
        [self.navigationController pushViewController:userLikeThingVC animated:YES];
    }
    
}

#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView.tag == 101) {     //如果是文章页
        return _articleDataArray.count;
    } else if (tableView.tag == 102) {    //如果是问题页
        return _questionDataArray.count;
    } else {
        return -1;
    }
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return 100;
//}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UserLikeTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:kUserLikeTableViewCellId forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor clearColor];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    if (tableView.tag == 101) {            //如果是文章页
        cell.dataDic = _articleDataArray[indexPath.row];
    } else if (tableView.tag == 102){      //如果是问题页
        cell.dataDic = _questionDataArray[indexPath.row];
    } else {
        cell = nil;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (tableView.tag == 101) {        //如果是文章页
        NSDictionary* dic = _articleDataArray[indexPath.row];
        
        UserLikeArticleViewController* userLikeArticleVC = [[UserLikeArticleViewController alloc] init];
        userLikeArticleVC.dataDic = dic;
        [self.navigationController pushViewController:userLikeArticleVC animated:YES];
        
    } else {                                //如果是问题页
        NSDictionary* dic = _questionDataArray[indexPath.row];
        
        UserLikeQuestionViewController* userLikeQuestionVC = [[UserLikeQuestionViewController alloc] init];
        userLikeQuestionVC.dataDic = dic;
        [self.navigationController pushViewController:userLikeQuestionVC animated:YES];
    }
}

//#pragma mark - scrollViewDelegate
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//
//    CGFloat y = scrollView.contentOffset.y;
////    NSLog(@"%f", y);
//    if (y < headImgViewHeight-30-navBarHeight) {
//        [headImgView setFrame:CGRectMake(0, -y, kWidth, headImgViewHeight)];
//        self.navigationController.navigationBar.alpha = y/(headImgViewHeight-30-navBarHeight);
//    }
//    
//}




@end
